package com.example.administrator.test;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.example.administrator.test.fragment.FragmentDrivingService;
import com.example.administrator.test.fragment.FragmentSpecial;
import com.example.administrator.test.fragment.FragmentTruck;
import com.example.administrator.test.login.RecordSQLiteOpenHelper;
import com.example.administrator.test.personal.IssueInvoiceActivity;
import com.example.administrator.test.personal.MyFansActivity;
import com.example.administrator.test.personal.MyOrderActivity;
import com.example.administrator.test.personal.MyWalletActivity;
import com.example.administrator.test.personal.PromotionActivity;
import com.example.administrator.test.personal.SettingsActivity;


public class MainActivity extends AppCompatActivity{
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private DrawerLayout mDrawer;
    private LinearLayout llDrawer;
    private TextView tv_personal_myfans,tv_personal_myRoute,tv_personal_myWallet,
            tv_personal_promotionCommission, tv_personal_myCustomerService, tv_personal_Setting;
    private Intent intent;
    private SQLiteDatabase db;
    private RecordSQLiteOpenHelper helper=new RecordSQLiteOpenHelper(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        //得到跳转时传递过来的登录信息以及手机号码
        Intent mIntentToMain=getIntent();
        String token=mIntentToMain.getStringExtra("token");
        String phone=mIntentToMain.getStringExtra("phone");
        //将用户信息存储到数据库中
        db = helper.getWritableDatabase();
        db.execSQL("insert into records(token) values('" + token + "')");//将登录密匙存入数据库
        db.execSQL("insert into records(phone) values('" + phone + "')");//将手机号码存入数据库
        db.close();
        //打开app默认加载专车界面
        manager=getSupportFragmentManager();
        transaction=manager.beginTransaction();
        transaction.add(R.id.layout_fragment,new FragmentSpecial());
        transaction.commit();
        mDrawer= (DrawerLayout) findViewById(R.id.dl_main);
        llDrawer= (LinearLayout) findViewById(R.id.ll_personal);
        ViewGroup.LayoutParams layoutParams=llDrawer.getLayoutParams();
        layoutParams.width=getResources().getDisplayMetrics().widthPixels*4/5;
        llDrawer.setLayoutParams(layoutParams);
        tv_personal_myfans= (TextView) findViewById(R.id.tv_personal_myFans);
        //点击跳转到我的粉丝页面
        tv_personal_myfans.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(MainActivity.this,MyFansActivity.class);
                startActivity(intent);
            }
        });
        //点击跳转到我的订单页面
        tv_personal_myRoute= (TextView) findViewById(R.id.tv_personal_myRoute);
        tv_personal_myRoute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(MainActivity.this,MyOrderActivity.class);
                startActivity(intent);
            }
        });
        //跳转到我的钱包
        tv_personal_myWallet= (TextView) findViewById(R.id.tv_personal_myWallet);
        tv_personal_myWallet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(MainActivity.this,MyWalletActivity.class);
                startActivity(intent);
            }
        });
        //点击跳转到推广佣金界面
        tv_personal_promotionCommission= (TextView) findViewById(R.id.tv_personal_promotionCommission);
        tv_personal_promotionCommission.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(MainActivity.this,PromotionActivity.class);
                startActivity(intent);
            }
        });
        //点击跳转到我的客服界面
        tv_personal_myCustomerService= (TextView) findViewById(R.id.tv_personal_myCustomerService);
        tv_personal_myCustomerService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(MainActivity.this,PromotionActivity.class);
                startActivity(intent);
            }
        });
        tv_personal_Setting= (TextView) findViewById(R.id.tv_personal_Setting);
        tv_personal_Setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(MainActivity.this,SettingsActivity.class);
                startActivity(intent);
            }
        });

    }
    //点击radiobutton动态切换专车、代驾、货车三个界面
    public void isChecked(View view){
        switch (view.getId()){
            case R.id.rb_special:
                manager=getSupportFragmentManager();
                transaction=manager.beginTransaction();
                transaction.replace(R.id.layout_fragment,new FragmentSpecial());
                transaction.commit();
                break;
            case R.id.rb_driving:
                manager=getSupportFragmentManager();
                transaction=manager.beginTransaction();
                transaction.replace(R.id.layout_fragment,new FragmentDrivingService());
                transaction.commit();
                break;
            case R.id.rb_truck:
                manager=getSupportFragmentManager();
                transaction=manager.beginTransaction();
                transaction.replace(R.id.layout_fragment,new FragmentTruck());
                transaction.commit();
                break;
        }
    }
    //点击主界面右上方message按钮跳转到消息界面/
    public void clickMessage(View view){
        intent=new Intent(MainActivity.this,MessageActivity.class);
        startActivity(intent);
    }
    public void clickPersonal(View view){
        mDrawer.openDrawer(llDrawer);
    }
    public void clickIssueInvoice(View view){
        intent=new Intent(MainActivity.this,IssueInvoiceActivity.class);
        startActivity(intent);
    }
    public void clickScore(View view){
        Toast.makeText(this,"该模块正在开发中，敬请期待!",Toast.LENGTH_SHORT).show();
    }
}
